package com.aos.iot.socket.tcp.config;

import com.aos.iot.socket.tcp.SocketTcpServer;
import com.aos.iot.socket.tcp.properties.SocketTcpProperties;
import com.aos.iot.socket.tcp.runner.SocketTcpRunner;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan(basePackages = "com.aos.iot.socket.tcp")
public class TcpConfiguration {

    @Bean
    public EventLoopGroup tcpBossGroup() {
        return new NioEventLoopGroup();
    }

    @Bean
    public EventLoopGroup tcpWorkerGroup() {
        return new NioEventLoopGroup();
    }

    @Bean
    public SocketTcpServer socketTcpServer(EventLoopGroup tcpBossGroup, EventLoopGroup tcpWorkerGroup,
                                           SocketTcpProperties socketTcpProperties,
                                           ChannelInitializer<? extends Channel> channelInitializer) {
        return new SocketTcpServer(tcpBossGroup, tcpWorkerGroup, channelInitializer, socketTcpProperties);
    }

    @Bean(
            destroyMethod = "stop"
    )
    public SocketTcpRunner socketTcpRunner(SocketTcpServer socketTcpServer) {
        return new SocketTcpRunner(socketTcpServer);
    }

}
